package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;

@RestController
@RequestMapping("setmeal")
public class SetmealMobileController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;

    /**
     * 查询所有套餐列表,添加缓存
     * @return
     */
    @RequestMapping("findSetmealAll")
    public Result findSetmealAll(){

        try {
            //01获取key
            String key = RedisConstant.SETMEAL_LIST_RESOURCES;
            String value = jedisPool.getResource().get(key);
            //02:判断key是不是等于null
            if (value != null) {
                //03获取key对应的value
                //把value返回到前台
                return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, value);
            }
            //05如果没有key的情况
            //5-1查询数据
            List<Setmeal> setmeal = setmealService.findSetmealAll();
            //5-2转换为json
            String json = JSON.toJSONString(setmeal);
            //5-3把json存入到redis
            jedisPool.getResource().set(RedisConstant.SETMEAL_LIST_RESOURCES, json);
            //5-4返回到前台
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, json);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }

    }

    /**
     * 通过套餐id查询套餐、套餐对应的检查组，以及检查组对应的检查项数据
     */
    @RequestMapping("findById")
    public Result findById(@RequestParam("id")Integer id){
        try {
            //01获取keysetmealListResources
            String key = RedisConstant.SETMEAL_DETAIL_RESOURCES;
            String value = jedisPool.getResource().get(key);
            //判断当前key是否存在
            if (value != null) {
                //就从redis中取
                //* String setmeals = jedisPool.getResource().get(key);*//*
                //返回到页面
                return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, value);
            }
            //如果,redis中没有就去数据库查询
            Setmeal setmeals = setmealService.findById(id);
            //查询好之后,存入redis中
            //把存入的数据转换成json
            jedisPool.getResource().set(key,JSON.toJSONString(setmeals));
            //返回成功页面
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeals);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_SUCCESS);
        }
    }

}
